package base.common;

import com.utils.exceptinos.OutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                         Exception ex) {
        System.out.println("全局异常处理==============common=================");

//        ex.printStackTrace();
        log.error(ex.getMessage(), ex);
        ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
        mav.addObject("code", "9999");
        if (ex instanceof OutException) {
            mav.addObject("msg", ex.getMessage());
            mav.addObject("data", ex.getMessage());
        }
        else {
            mav.addObject("msg", "访问出错");
            mav.addObject("data", "访问出错");
        }
        return mav;
    }
}

